﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using BuXiaChu.OrderDishes.BLL.WebRuntime;
using BuXiaChu.OrderDishes.BLL;
using ModulesNS = BuXiaChu.OrderDishes.Web.ServiceStation.Modules;
using BuXiaChu.OrderDishes.BLL.Constants;
using BuXiaChu.OrderDishes.Model;

namespace BuXiaChu.OrderDishes.Web.ServiceStation
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class List : HttpHandlerBase, IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            base.Init(context);
            var t = GetFormValue("t");
            switch (t)
            {
                case "1":
                    GetOrderList();
                    break;
                case "2":
                    GetOrderItemDetails();
                    break;
                case "3":
                    GetEmployeeList();
                    break;
                default:
                    AjaxActionResult result = new AjaxActionResult();
                    result.message = "未知的操作 t:" + t;
                    SerializeDataToResponseStream(result);
                    break;
            }
        }

        private void GetOrderList()
        {
            #region
            AjaxActionResult result = new AjaxActionResult();
            try
            {
                UserControlPreviewManager<ModulesNS.OrderList> preivewManager = new UserControlPreviewManager<ModulesNS.OrderList>();
                ModulesNS.OrderList dataListControl = preivewManager.LoadUserControl("~/Modules/OrderList.ascx");
                dataListControl.OrderId = GetFormValue("orderId");
                dataListControl.CallerId = GetFormValue("callerId");
                dataListControl.Address = GetFormValue("address");
                dataListControl.OrderStatus = GetFormValue("status");
                dataListControl.DeliveryManId = GetFormValue("empId");

                result.message = preivewManager.RenderHtml(dataListControl);
                result.success = true;
            }
            catch (Exception ex)
            {
                ExceptionMessageManager.Record(ex);
            }

            SerializeDataToResponseStream(result);
            #endregion
        }

        private void GetOrderItemDetails()
        {
            #region
            AjaxActionResult result = new AjaxActionResult();
            try
            {
                UserControlPreviewManager<ModulesNS.OrderDetails> preivewManager = new UserControlPreviewManager<ModulesNS.OrderDetails>();
                ModulesNS.OrderDetails dataListControl = preivewManager.LoadUserControl("~/Modules/OrderDetails.ascx");
                dataListControl.OrderId = long.Parse(GetFormValue("orderId"));

                result.message = preivewManager.RenderHtml(dataListControl);
                result.success = true;
            }
            catch (Exception ex)
            {
                ExceptionMessageManager.Record(ex);
            }

            SerializeDataToResponseStream(result);
            #endregion
        }

        private void GetEmployeeList()
        {
            #region
            AjaxActionResult result = new AjaxActionResult();
            try
            {
                var empList = EmployeesManager.GetEmployeesByJobTitle(CodeDictionaryConstants.DeliveryGuys);
                var tempList = new List<object>();
                empList.ForEach(
                        item =>
                        {
                            if (item.Status != 255)
                            {
                                tempList.Add(new { id = item.Id, name = item.Name });
                            }
                        }
                    );
                result.obj = tempList;
                result.success = true;
            }
            catch (Exception ex)
            {
                ExceptionMessageManager.Record(ex);
            }
            SerializeDataToResponseStream(result);
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
